﻿using System.IO;
using Server;
using System;
using Server.Packets;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;

namespace GetThis_Server.Packets
{
    public class C1HandshakePacket : Packet
    {
        #region Constants

        /// <summary>
        /// Default user name.
        /// </summary>
        private const string DefaultUserName = "Finnithnel";

        #endregion

        #region Fields

        /// <summary>
        /// User name.
        /// </summary>
        private string m_userName;

        #endregion

        #region Constructors

        /// <summary>
        /// Define the invariant valid state for this class.
        /// </summary>
        [ContractInvariantMethod]
        private void InvariantValidState()
        {
            Contract.Invariant(!String.IsNullOrWhiteSpace(m_userName));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="T:GetThis_Server.Packets.C1HandshakePacket" /> class.
        /// </summary>
        public C1HandshakePacket()
        {
            m_userName = DefaultUserName;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Identifier.
        /// </summary>
        protected override byte Id
        {
            get { return 1; }
        }

        /// <summary>
        /// User name.
        /// </summary>
        public string UserName
        {
            get
            {
                Contract.Ensures(!String.IsNullOrWhiteSpace(Contract.Result<string>()));

                return m_userName;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Reads the raw packet data from the data reader.
        /// </summary>
        /// <param name="reader">Data reader.</param>
        protected override void ReadPacketData(BinaryReader reader)
        {
            Contract.Ensures(!String.IsNullOrWhiteSpace(m_userName));

            var userName = reader.ReadString();
            if (String.IsNullOrWhiteSpace(userName))
                throw new InvalidDataException("The user name read was null or empty, or if value consists exclusively of white-space characters.");
            m_userName = userName;
        }

        /// <summary>
        /// Write the raw packet data to the data writer.
        /// </summary>
        /// <param name="writer">Data writer.</param>
        protected override void WritePacketData(BinaryWriter writer)
        {
            Contract.Assert(!String.IsNullOrWhiteSpace(m_userName));

            writer.Write(m_userName);
        }

        /// <summary>
        /// Converts this instance to its equivalent string representation.
        /// </summary>
        /// <returns>The string representation of the value of this instance.</returns>
        public override string ToString()
        {
            return String.Format("C1HandshakePacket - user name : {0}", m_userName);
        }

        #endregion
    }
}
